package org.esk.dablog.gwt.dablog.client;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Event;
import org.esk.dablog.gwt.dablog.client.model.ForumEntry;


/**
 * This class 
 * User: esk
 * Date: 10.01.2007
 * Time: 10:17:38
 * $Id:$
 */
public class ForumEntryWidget extends Composite {

    private Label header = new Label();
    private Label info = new Label();

    private Panel textPane = new VerticalPanel();
    private Label text = new Label();
    private Label answer = new Label("answer message");
    private Panel answerPane = new VerticalPanel();
    private boolean expanded = false;
    private boolean answerVisible = false;

    private ForumEntry entry;

    public ForumEntryWidget(ForumEntry entry) {
        this.entry = entry;
        collapse();
        VerticalPanel panel = new VerticalPanel();
        DockPanel head = new DockPanel() {

            public void onBrowserEvent(Event event) {
                switch (DOM.eventGetType(event)) {
                    case Event.ONCLICK:
                        onClick();
                        break;

                    case Event.ONMOUSEDOWN:
                    case Event.ONMOUSEUP:
                    case Event.ONMOUSEMOVE:
                    case Event.ONMOUSEOVER:
                    case Event.ONMOUSEOUT:
                        onMouseEvent(this, event);
                        break;
                }
            }
        };
        head.sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS);
        info.setText(entry.getAuthorUsername());

        DOM.setStyleAttribute(header.getElement(), "whiteSpace", "normal");
        DOM.setStyleAttribute(text.getElement(), "whiteSpace", "normal");

        head.setStyleName("dablog-ForumEntryHeader");
        textPane.setStyleName("dablog-ForumEntryText");
        info.setStyleName("dablog-ForumEntryInfo");

        head.add(header, DockPanel.CENTER);
        head.add(info, DockPanel.EAST);
        head.setWidth("100%");
        answer.addClickListener(new ClickListener(){
            public void onClick(Widget sender) {
                answerPaneVisible();
            }
        });
        panel.add(head);
        panel.add(textPane);

        textPane.setVisible(false);
        textPane.add(text);
        textPane.add(answer);
        textPane.add(answerPane);

        initAnswerPane();
        initWidget(panel);
        setStyleName("dablog-ForumEntry");
    }

    /**
     * initializes "answerPane"
     */
    private void initAnswerPane() {
        answerPane.setVisible(false);
        answerPane.setWidth("100%");
        TextArea area = new TextArea();
        area.setWidth("100%");
        Button b = new Button("post answer");
        answerPane.add(area);
        answerPane.add(b);
    }

    /**
     * sets anser pane visible
     */
    private void answerPaneVisible() {
        answerVisible = !answerVisible;
        answerPane.setVisible(answerVisible);
    }

    /**
     * mouse click handler
     */
    private void onClick() {
        expanded = !expanded;
        if (expanded) {
            expand();
        } else {
            collapse();
        }
    }

    /**
     * mouse event handler
     * @param dockPanel
     * @param event
     */
    private void onMouseEvent(DockPanel dockPanel, Event event) {


    }

    /**
     * expands the widget area
     */
    public void expand() {
        if (entry.hideHeaderOnExpand()) {
            DOM.setInnerHTML(this.header.getElement(), "");
        }
        DOM.setInnerHTML(this.text.getElement(), entry.getText());
        textPane.setVisible(true);
    }

    /**
     * collapses the widget area
     */
    public void collapse() {
        DOM.setInnerHTML(this.text.getElement(), "");
        DOM.setInnerHTML(this.header.getElement(), entry.getHeader());
        textPane.setVisible(false);
    }
}
